A = imread('cameraman.tif');
B = imnoise(A,'salt & pepper',0.04);
[row,col] = size(A);
figure, imshow(A);
figure, imshow(B);
C = padarray(A,[1,1]);
meanmatrix = 1/9*[1 1 1; 1 1 1; 1 1 1];
for  i = 2: (row-1)
   for j = 2 : (row-1) 
       sum = 0;
       for k = 1 : 3
           for l = 1 : 3
               sum  = sum + (C((i-2)+k,(j-2)+l)*meanmatrix(k,l));
           end
       end
       C(i,j) = sum;
end
end
figure,imshow(C);